<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 获取搜索参数
    $searchParam = $_GET['search'] ?? '';

    // 构造模糊搜索 SQL 语句
    $sql = "SELECT * FROM mi_users 
            WHERE 账号 LIKE :search 
               OR 昵称 LIKE :search 
               OR 邮箱 LIKE :search";
    $stmt = $pdo->prepare($sql);
    $searchValue = '%' . $searchParam . '%';
    $stmt->bindParam(':search', $searchValue, PDO::PARAM_STR);
    $stmt->execute();

    // 获取查询结果
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // 输出结果
    echo json_encode($results, JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    // 捕获异常并返回错误信息
    $errorResult = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($errorResult, JSON_UNESCAPED_UNICODE);
}
?>
